/*
 * @Author: your name
 * @Date: 2021-11-02 10:16:12
 * @LastEditTime: 2021-11-02 11:19:31
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /websocket/app.js
 */
const WebSocket = require('ws');

const ws = new WebSocket.Server({
    port: 9999
})

let scokets = [];// 记录用户的 记录socket
ws.on('connection', (socket) => {
    // 讲客户端每次发送的消息 记录下来
    scokets.push(socket);
    socket.on('message', (msg) => {
        // 判断当前的socket 是否存在scokets数组中
        scokets.forEach(item => {
            if(item !== socket) {
                // 客户端发送来的消息通知
                item.send(msg);
            }
            
        })
    })
})